package com.ruipeng.shengyapo.locksocket;

import java.util.Map;
import java.util.TreeMap;

public class LockManager {

	// 开锁指令
	public static byte[] getOpenLockCmd(String lockIMEI) {
		byte[] order = ("*CMDS,XX," + lockIMEI + ",000000000000,L0,0#").getBytes();
		return addByte(new byte[] { (byte) 0xff, (byte) 0xff }, order);
	}
	
	// OM测试开锁指令
	public static byte[] getOmOpenLockCmd(String lockIMEI) {
		byte[] order = ("*CMDS,OM," + lockIMEI + ",000000000000,L0,0#").getBytes();
		return addByte(new byte[] { (byte) 0xff, (byte) 0xff }, order);
	}
	
	// 获取定位指令
	public static byte[] getLocationCmd(String lockIMEI) {
		byte[] order = ("*CMDS,XX," + lockIMEI + ",000000000000,D0#").getBytes();
		return addByte(new byte[] { (byte) 0xff, (byte) 0xff }, order);
	}
	
	// 开锁指令
	public static byte[] getCardICCIDCmd(String lockIMEI) {
		byte[] order = ("*CMDS,OM," + lockIMEI + ",000000000000,I0#").getBytes();
		return addByte(new byte[] { (byte) 0xff, (byte) 0xff }, order);
	}
	
	// 关机指令
	public static byte[] getClosedownLockCmd(String lockIMEI) {
		byte[] order = ("*CMDS,OM," + lockIMEI + ",000000000000,S0#").getBytes();
		return addByte(new byte[] { (byte) 0xff, (byte) 0xff }, order);
	}

	public static Map<String, String> analyseOrder(String order) {
		// *CMDR,XX,863158022988725,161201150000,Q0,412#
		String[] orders = order.split(",");
		if (orders.length < 5) {
			return null;
		}
		Map<String, String> orderMap = new TreeMap<String, String>();
		orderMap.put("DeviceIMEI", orders[2]);
		orderMap.put("OrderType", orders[4]);

		return orderMap;
	}

	private static byte[] addByte(byte[] a, byte[] b) {
		byte[] c = new byte[a.length + b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		return c;
	}

}
